//logs.js
const util = require('../../utils/util.js')

Page({
	data: {
		logs: [],
		a: 'false',
		tjr: ''
	},
	onLoad: function(options) {
		const scene = decodeURIComponent(options.scene);
		if (scene) {
			console.log("scene is ", scene);
			var arrPara = scene.split(",");
			console.log(arrPara)
			wx.request({
				url: getApp().globalData.http + 'action=setTjr&tjr=' + arrPara[0],
				success: (e) => {
					console.log(e)
					if (e.data.code == 1) {
						this.setData({
							tjr: e.data.data.code,
						})
					}else{
						
					}
				}
			})
		}
	},
	a(e) {
		let that = this
		let key = ''
		wx.login({
			success: function(res) {
				console.log(res)
				if (res.code) {
					wx.request({
						url: getApp().globalData.map + "/tools/weixinShouQuan.aspx?code=" + res.code,
						success: function(n) {
							console.log(n)
							wx.setStorage({
								key: 'key',
								data: n.data.session_key,
							})
							key = n.data.session_key
							wx.getUserInfo({
								success: function(res) {
									console.log(res)
									wx.setStorage({
										key: 'state',
										data: res,
									})
									wx.request({
										url: getApp().globalData.http + "action=wxLogin&skey=" + key + "&encryptedData=" +
											res.encryptedData + "&iv=" + res.iv,
										success: function(e) {
											console.log(e)
											wx.setStorage({
												key: 'user',
												data: e.data.data,
											})
											wx.request({
												url: getApp().globalData.http + "action=wxlogin&unionid=" + e.data.data.unionId,
												success: function(res) {
													console.log(res)
													if (res.data.code == 1) {
														wx.setStorage({
															key: 'uid',
															data: res.data.data
														})
														let a = getCurrentPages()[getCurrentPages().length - 2]
														console.log(that.data.tjr)
														if (that.data.tjr != '') {
															console.log('成功')
															wx.redirectTo({
																url: '../set/me/tjr/tjr?tjr='+that.data.tjr
															})
														} else {
															console.log('失败')
															if (a == undefined) {
																wx.switchTab({
																	url: '../set/set'
																})
															} else {
																wx.navigateBack({
																	delta: 1,
																})
															}
														}

													} else {
														wx.navigateTo({
															url: '../logs/logs?id=' + that.data.tjr
														})
													}
												}
											})
										}
									})
								}
							})
						}
					})
				}
			}
		})
	},
})
